/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2019-2019.
 * Description: support for hi56t1t
 * Author: yanbo <joey.yanbo@huawei.com>
 * Create: 2019-08-11
 */
#include <linux/types.h>
#include <linux/libfdt.h>
#include <linux/hal32/hal_reserved_memory_map.h>

#define HI5651T_BUG_PROPERTY_NANE "hi5651t_add_outer_shareable"
#define ALIGE_1M_MASK 0xfff00000

void set_hi5651_magic(unsigned char *image_addr, unsigned int hi5651_magic)
{
	unsigned int *magic_addr = NULL;

	magic_addr = (int *)((uintptr_t)image_addr & ALIGE_1M_MASK);
	magic_addr = (int *)((uintptr_t)magic_addr + HI5651T_MAGIC_OFFSET);

	*magic_addr = hi5651_magic;
}

void hi5651t_bugfix(const void *dtb_address, unsigned char *image_addr)
{
	int node;
	unsigned int size;
	unsigned hi5651_magic = 0;
	const struct fdt_property *prop = NULL;

	node = fdt_node_offset_by_prop_value(dtb_address, -1, "compatible",
			"cpu_info,extra_info", sizeof("cpu_info,extra_info"));
	if (node < 0)
		return;

	prop = fdt_getprop_namelen(dtb_address, node, HI5651T_BUG_PROPERTY_NANE,
				strlen(HI5651T_BUG_PROPERTY_NANE), &size);
	if (prop)
		hi5651_magic = HI5651T_MAGIC;

	set_hi5651_magic(image_addr, hi5651_magic);
}
